//
//  PersonalHomePageViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/16.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "PersonalHomePageViewController.h"

#import "LeftView.h"

@interface PersonalHomePageViewController ()

@property (nonatomic, strong) LeftView * leftView; // 左侧视图
@property (nonatomic, strong) NSArray * vcNames; // 类名数组

@end

@implementation PersonalHomePageViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 添加视图
    [self.view insertSubview:self.leftView atIndex:999];
    
    // 默认加载第一个视图与控制器
    UIViewController * courseVc = [[NSClassFromString(self.vcNames[0]) alloc] init];
    [self addChildViewController:courseVc];
    [self.view insertSubview:courseVc.view belowSubview:_leftView];
}
- (void)viewWillAppear:(BOOL)animated {
    // 自动布局
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.195);
        make.left.offset(0);
        make.top.offset(SCREEN_HEIGHT * 0.092);
        make.bottom.offset(SCREEN_HEIGHT * -0.09);
    }];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (LeftView *)leftView {
    if (!_leftView) {
        _leftView = [[LeftView alloc] initWithSelecBtnBlock:^(NSInteger index) {
            // 获取到最上层显示的视图及其控制器
            NSArray * subviews = self.view.subviews;
            UIView * topView = subviews[subviews.count - 2];
            UIViewController * topVc = self.childViewControllers.lastObject;
            
            // 获取当前选择的视图控制器
            UIViewController * currenVc = [[NSClassFromString(self.vcNames[index]) alloc] init];
            
            // 切换视图控制器
            [topView removeFromSuperview];
            [topVc removeFromParentViewController];
            [self addChildViewController:currenVc];
            [self.view insertSubview:currenVc.view belowSubview:_leftView];
        }];
    }
    return _leftView;
}
- (NSArray *)vcNames {
    if (!_vcNames) {
        _vcNames = @[@"CourseViewController", @"NoteAndQuestionAnswerViewController", @"CollectionViewController", @"NoteAndQuestionAnswerViewController", @"SystemMessageViewController", @"SchollFriendManagerViewController", @"ActivityViewController", @"PostViewController", @"DownloadViewController", @"SetupViewController"];
    }
    return _vcNames;
}

@end
